<?php 
	//timer pengecekan load page...
	include("inc/timer.php");
	$_start_timer = get_microtime();
	 
	//check protect1 jika session da tidak valid, balikin ke index.php
	//jika valid y biarin saja
	
	require_once("protect1.php");
	require_once("inc/connect.php");
	require_once("inc/settings.php");
	require_once("inc/function.php");
	require_once("inc/function-html.php");

	// $pilih=mysql_select_db("uwika_krs",$conn);
	//$pdo->query("USE uwika_krs");

	//pindahkan ke lokal variable
	$_nrp = $reg_uname;
	$_semester = $_global_settings['semester']->value;
	$_tahun_ajaran = $_global_settings['tahun_ajaran']->value;						

	$sql = "SELECT nama FROM biodata WHERE nrp = :reg_uname ";
	$stmt_mahasiswa = $pdo->prepare($sql);
	$stmt_mahasiswa->execute(array(':reg_uname' => $reg_uname )) or die("ERROR: " . implode(":", $stmt_mahasiswa->errorInfo())); //reg jurusan dari protect1.php
	$_data_mahasiswa = $stmt_mahasiswa->fetch(PDO::FETCH_OBJ);

	$periode_array = array('1-00-01', '2/00-01'
					 ,'1/01-02', '2/01-02'
					 ,'1/02-03', '2/02-03'
					 ,'1/03-04', '2/03-04'
					 ,'1/04-05', '2/04-05'
					 ,'1/05-06', '2/05-06'
					 ,'1/06-07', '2/06-07'
					 ,'1/07-08', '2/07-08'
					 ,'1/08-09', '2/08-09'
					 ,'1/09-10', '2/09-10'
					 ,'1/10-11', '2/10-11'
					 ,'1/11-12', '2/11-12'
					 ,'1/12-13', '2/12-13'
					 ,'1/13-14', '2/13-14'
					 ,'1/14-15', '2/14-15'
					 ,'1/15-16', '2/15-16');

	//get jurusan & dosen wali
	require_once("inc/function-db.php");
	
	$_jurusan = $function_db->getJurusan(substr($_nrp,0,3),$pdo);
	$_dosen_wali = $function_db->getDosenWali($_nrp,$pdo);

?>
<!DOCTYPE html>
<html>
<head>
<title>Student Home !</title>
<link rel="stylesheet" type="text/css" href="css/reset_main.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<style type="text/css">
	body{ behavior:url(cssHoverFix.htc);}
	#div a { normal state }
	#div a:hover { alternative state }
</style>
</head>
<body>	
	<header id="main">
		<table id="header">
			<tr>
				<td class="header_logo_small"><a class="header_logo_small" href="#"></a></td>
				<td class="header_announce">
					<marquee style="letter-spacing:1px;marquee-speed:slow;">
						Nilai <strong>UTS</strong> Semester Gasal Periode 2012-2013 sudah keluar *sebagian. 
						Jika anda menemukan Bug / Kritik / Saran, silahkan Laporkan kepada ICT Sesegera mungkin. 
						Terima Kasih.
					</marquee>
				</td>
				<td class="header_user_name"><a class="header_user_name_a" href="#"><?php echo (strlen($_data_mahasiswa->nama) < 15 ? ucwords(strtolower($_data_mahasiswa->nama)) : ucwords(strtolower(substr($_data_mahasiswa->nama,0,15)))."..."); ?></a></td>
				<td class="header_user_photo">
					<a class="header_user_photo_a" href="#">
						<img class="header_user_photo_img" src="images/people/default.gif">
					</a>
				</td>
			</tr>
		</table>
	</header>
	<table class="board">
		<tr>
			<td class="sidebar">
				<div id="navigation">
					<ul class="top-level">
						<?php
							$_level_settings = getLevelSettings($reg_level, $pdo);
							$basename_url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
							if(count($_level_settings->menu) > 0){
								foreach($_level_settings->menu as $foreach){
									if ($basename_url == $foreach->url){$clicked = 'class="clicked"';}else{$clicked = '';}
									echo '<li><a href="'.$foreach->url.'" '.$clicked.'>'.$foreach->nama.'</a></li>';
								}
							}else{ echo '<li><a href="#" class="clicked">Home</a></li>'; }
						?>
					</ul>
				</div>
			</td>
			<td class="bigbox">
				<div class="banner"></div>
				<div class="content letter-spacing">
					<br \>
					<h1>Hasil KHS Anda</h1><br \>	
					<h2>Dibawah ini Nilai KHS <span class="sign blue border">[Transkrip Nilai]</span> Anda yang telah anda tempuh selama ini</h2><br \>
					<table class="data head ">
						<tr>
							<td width="15%">Periode</td><td width="2%">:</td><td width="35%"><?php echo $_tahun_ajaran; ?></td><td width="15%">NRP</td><td width="2%">:</td><td width="35%"><?php echo $_nrp; ?></td>
						</tr>
						<tr>
							<td>Semester</td><td>:</td><td><?php echo $_semester; ?></td><td>Nama</td><td>:</td><td><?php echo $_data_mahasiswa->nama; ?></td>
						</tr>
						<tr>
							<td>Program Studi</td><td>:</td><td><?php echo strtoupper($_jurusan->nama_jurusan); ?></td><td>Dosen Wali</td><td>:</td><td><?php echo $_dosen_wali->nama; ?></td>
						</tr>
					</table><br \>
					<!-- garis abu2 -->
					<table class="data body"><tfoot><tr><td></td></tr></tfoot></table>
					
					<?php
					$count_semester = 1;
					for($i = 0; $i < count($periode_array) ; $i++ ){
					//	$sql = "SELECT mk.kodemk, nama, krs.sks, NA from krs, mk WHERE nrp=:nrp AND periode= :periode AND mk.kodemk = krs.kode";
						$sql = "SELECT kodemk, nama, sks, nilai from sks WHERE nrp=:nrp AND periode=:periode";
						$stmt_khs = $pdo->prepare($sql);
						$stmt_khs->execute(array(':nrp' => $_nrp
											,':periode' => $periode_array[$i] )) or die("ERROR: " . implode(":", $stmt_khs->errorInfo()));
						if($stmt_khs->rowCount() > 0){
							$table_colour = 0;
							//
							$jmlsks = 0;
							$totbobot = 0;
							$periode_semester_pecah = $function->periodelama2periodensemester($periode_array[$i]);
							//
							//cetak tabel header
							?>
							<table class="data head ">
								<tr>
									<td width="12%">Periode	</td><td width="2%">:</td><td width="61%"><?php echo $periode_semester_pecah->periode; ?></td>
									<td width="12%">Semester</td><td width="2%">:</td><td><?php echo $periode_semester_pecah->semester; ?> <span class="sign border <?php echo $function_html->getWarnaSemester($count_semester); ?>"><?php echo $count_semester; ?></span></td>
								</tr>
							</table><br \>
							
							<table class="data body">
								<thead>
									<th width="4%">No</th><th width="8%">Kode</th><th colspan="3">Mata Kuliah</th><th>SKS</th><th width="7%">Grade</th>
								</thead>
								<tbody>
							<?php
							$count = 1; //using odd php for css2 browser and less
							while($_data_khs = $stmt_khs->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
								//cetak isi
								?>
									<tr onclick="document.location.href='#';" <?php echo ( ($count % 2) == 0 ? 'class="odd"' : ''); ?>>
										<td class="center"><a id="block" href="#"><?php echo $count; ?></a></td>
										<td class="center"><?php echo $_data_khs->kodemk ?></td>
										<td colspan="3" align="left"><?php echo $_data_khs->nama; ?></td>
										<td class="center"><?php echo $_data_khs->sks ?></td>
										<td class="center" style="font-weight:bolder;">
										<?php 
											if ($_data_khs->nilai != 'BL') {
												$jmlsks = $jmlsks + $_data_khs->sks;
												$totbobot = $totbobot + ($function->huruf2angka($_data_khs->nilai) * $_data_khs->sks);
												if ($_data_khs->nilai == 'E' || $_data_khs->nilai == 'D'){
													echo '<span class="sign red border">'.$_data_khs->nilai."</span>";
												}else{
													echo $_data_khs->nilai;
												}
											}else{
												echo "<code>".$_data_khs->nilai."</code>";
											}
										?>
										</td>
									</tr>
								<?php
								//cetak finish
								$count++;
							}

							$count_semester++;
							//memperbaiki error division by zero saat totalSKS = 0, karna nilai mk belum lulus semua.
							?>
								</tbody>
								<tfoot>
									<tr>
										<td colspan="2">Nilai IPS</td><td style="text-align:left;"><span class="sign black big border"><?php if($jmlsks > 0){ echo round($totbobot / $jmlsks,2); } else { echo 0; } ?></span></td>
										<td></td><td style="text-align:right;">TOTAL</td><td><span class="sign black big border"><?php echo $jmlsks; ?></span></td><td></td>
									</tr>
								</tfoot>
								</table>
								<br \>
							<?php
						}
					}
					?>

					<?php

					$allnilai = 0;
					$allsks = 0;
					$sql = "SELECT kodemk, sks, nilai FROM `sks` WHERE nrp=:nrp Order by kodemk, nilai";
					
					// $sql = "SELECT distinct(kodemk),sks, nilai FROM `sks` WHERE nrp=:nrp"; 
					$stmt_mk_krs = $pdo->prepare($sql);
					$stmt_mk_krs->execute(array(':nrp' => $_nrp));
					$_data_mk_krs = $stmt_mk_krs->fetchAll(PDO::FETCH_OBJ);
					
					//hapus data ganda! dan pakai nilai tertinggi
					//ini bisa digunakan ketika nantinya sks dipakai sendiri. tetapi rasanya tidak... ~.~
					for($i = 0; $i <count($_data_mk_krs); $i++){
						for($j = 1; $j <count($_data_mk_krs) - 1; $j++){
							if( ($_data_mk_krs[$i]->kodemk == $_data_mk_krs[$j]->kodemk) && ($i!=$j) && (isset($_data_mk_krs[$i])) && (isset($_data_mk_krs[$j])) ){
								if($function->huruf2angka($_data_mk_krs[$i]->nilai) >= $function->huruf2angka($_data_mk_krs[$j]->nilai)){
									$_data_mk_krs[$j] = null;
								}else{
									$_data_mk_krs[$i] = null;
								}
							}
						}
					}
					
					//hitung nilai yg bukan bukan BL. setelah data dihapus yg ganda dan diambil nilai tertinggi
					foreach($_data_mk_krs as $_datafor){ //fetch data to stdclass object
						if(isset($_datafor)){
							if($_datafor->nilai != "BL"){
								$allsks = $allsks + $_datafor->sks;
								$allnilai = $allnilai + ($function->huruf2angka($_datafor->nilai) * $_datafor->sks);
							}
						}
					}
					?>
					<table class="data foot">
						<tbody>
							<tr>
							<tr><th width="38%">Total SKS yang sudah anda Tempuh</td><td width="1%">:</td><td>&nbsp;<span class="sign black big border"><?php echo $allsks; ?></span> SKS</td></tr>
							<tr><th>Nilai IPS <span class="sign blue border">[Index Prestasi Kumulatif]</span></td><td>:</td>
								<td>&nbsp;<?php if($allsks > 0){ echo round($allnilai / $allsks,3); } else { echo 0; } ?>
								</td></tr>
						</tbody>
					</table>
					<div class="warning blue">
						<label>*Info</label>
						Nilai diatas tidak dapat menjadi patokan pasti Nilai dan Jumlah SKS anda. Untuk hasil yang lebih Up2Date,
						Silahkan Hubungi <code>BAA</code>.
					</div><br \>
					<div style="padding-bottom:50px;"></div>
				</div>
			</td>
		</tr>
	</table>
	<footer id="main">
	  <div><a href="http://www.widyakartika.ac.id/perpustakaan">Perpustakaan Online</a> | <a href="http://krs.widyakartika.ac.id/">KRS Online</a></div>
	  <div>Copyright &copy; 2012. <a href="http://www.widyakartika.ac.id">Universitas Widya Kartika</a> & <a href="#">Labkom ICT</a> - All rights reserved</div>
		<?php
			$_finish_timer = get_microtime();
			echo 'Page generated in '.round(($_finish_timer - $_start_timer), 4).' seconds.';
		?>
	</footer>
</body>
